import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SIPEngine extends Thread{
	private int port;
	private ServerSocket server = null;
	private boolean serverstatus = false;
	private Socket socket = null;
	public final static int WAIT = 0;
	public final static int CONNECT = 1;
	
	public SIPEngine(){
		this.port = 9800;
		init();
	}
	
	
	public SIPEngine(int port){
		this.port = port;
		init();
	}
	
	private void init(){
		try {
			server = new ServerSocket(port);
			serverstatus = true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("SIPEngine Started Failed.");
			serverstatus = false;
		}
	}
	
	public boolean getEngineStatus(){
		return serverstatus;
	}
	
	public void closeEngine(){
		serverstatus = false;
	}
	
	public void run(){
		socket = new Socket();
		while(serverstatus){
			try {
				socket = server.accept();
				System.out.println("listening to a client...");
				SIPListener siplistener = new SIPListener(socket);
				
				siplistener.start();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			server.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Server Closed Failed");
		}
	}
	
	
	
}
